﻿Public Class clsAnimateImage
    'Inherits Form

    'Create a Bitmpap Object. 
    Private _animatedImage As Bitmap
    Private _picture As PictureBox
    Private _graphic As Graphics

    Public Sub New(p As PictureBox, gifImageFile As Bitmap)
        _picture = p
        _graphic = _picture.CreateGraphics()
        _animatedImage = gifImageFile
    End Sub
    'This method begins the animation. 
    Public Sub StartAnimation()

        'Begin the animation only once.
        ImageAnimator.Animate(_animatedImage, _
        New EventHandler(AddressOf Me.OnFrameChanged))
        _picture.Visible = True
        _graphic.Clear(_picture.BackColor)
    End Sub
    Public Sub StopAnimation()
        ImageAnimator.StopAnimate(_animatedImage, New EventHandler(AddressOf Me.OnFrameChanged))
        _picture.Visible = False
    End Sub
    Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)

        'Force a call to the Paint event handler. 
        'Me.Invalidate()
        'Get the next frame ready for rendering.
        ImageAnimator.UpdateFrames()

        'Draw the next frame in the animation.
        _graphic.Clear(_picture.BackColor)
        _graphic.DrawImage(_animatedImage, New Point(0, 0))
    End Sub

End Class
